<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    img {
      width: 400px;
      height: 300px;
    }
  </style>
</head>

<body>
  <p>1</p>
  <img src="./img/img1.jpg" alt="">
  <button>点击加载下一张</button>
  <script>
    const imgs = ["./img/img2.jpg", "./img/img3.jpg", "./img/img4.jfif", "./img/img5.jpg"];
    let img = document.querySelector('img')
    let p = document.querySelector('p')
    let btn = document.querySelector('button')
    let index = 0
    function preLoad (index) {
      let img = document.createElement('img')
      img.src = imgs[index]
    }
    preLoad(index)
    p.innerText = `这是第${index + 1}张图片`
    btn.addEventListener('click', () => {
      if (index < imgs.length) {
        img.src = imgs[index] // 显示这个预加载的图片

        index++ // 索引后移
        p.innerText = `这是第${index + 1}张图片`
        if (index < imgs.length) {
          preLoad(index)
        }
      } else {
        alert('没有图片了')
      }
    })
  </script>
</body>

</html>